package com.dai.zhenfujin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;



public class WelcomeActivity extends Activity {
    private ViewPager viewPager;

    private List<View> tabViews = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        viewPager = findViewById(R.id.view_pager);
        loadData();

    }


    protected void loadData() {
        //引入一个布局，三个共用，设置不同的背景图片
        LayoutInflater tabs = LayoutInflater.from(WelcomeActivity.this);
        View tab1 = tabs.inflate(R.layout.tab_03 ,null);
     //   tab1.setBackgroundResource(R.mipmap.page_one);

        View tab2 = tabs.inflate(R.layout.tab_01, null);
       // tab2.setBackgroundResource(R.mipmap.page_two);
        View tab3 = tabs.inflate(R.layout.tab_02, null);

      //  View tab4 = tabs.inflate(R.layout.tab_04, null);

        //添加到列表里
        tabViews.add(tab1);
        tabViews.add(tab2);
        tabViews.add(tab3);
      //  tabViews.add(tab4);


        //设置适配器
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return tabViews.size();//
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(tabViews.get(position));
                //滑到最后一个页面，显示button
                if (position == tabViews.size() - 1) {
                    Button bt_into = tabViews.get(position).findViewById(R.id.bt_into);
                    bt_into.setOnClickListener(view -> {
                       // startActivity(new Intent(WelcomeActivity.this,AccountPasswordActivity .class));
                        startActivity(new Intent(WelcomeActivity.this,MainActivity .class).putExtra("showRed",0));
                        finish();
                    });
                }
                return tabViews.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(tabViews.get(position));
            }
        });
    }
}
